/*
 * 抢庄
 * author: sfmy
 */
var NNModel           = require('./NNModel');
var NNProto           = require('./NNProto');
var RoomProto         = require('../../API/RoomProto');
var HallApi           = require('../../API/HallAPI');
var RoomAPI           = require('../../API/RoomAPI');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';

cc.Class({
	extends: cc.Component,
	properties: {
	},

	onLoad: function () {
		this.node.active = false;
		Global.MessageCallback.addListener('RoomMessagePush', this);
		Global.MessageCallback.addListener('GameMessagePush', this);
		Global.MessageCallback.addListener('ReConnectSuccess', this);
	},

	gameInit: function () {
		/* if (NNModel.getGameType() == NNProto.gameType.ZHIYOUQZ) {
			for (let i = 1; i <= 4; ++i) {
				cc.find('Button'+i, this.node).active = false;
			}
			cc.find('Button6', this.node).active = true;
		} */
		if (NNModel.getGameStatus() == NNProto.gameStatus.ROB_BANK && NNModel.getRobScaleByChairID(NNModel.getMyChairID()) == null && NNModel.getIsPlayingByChairID(NNModel.getMyChairID())) {
			this.node.active = true;
		}
		else {
			this.node.active = false;
		}
	},

	messageCallbackHandler: function(router, msg) {
		if(!NNModel.getGameInited()) { return; }
		if (router === 'RoomMessagePush') {
			if(msg.type === RoomProto.GET_ROOM_SCENE_INFO_PUSH) {
				this.gameInit();
			}
		}
		else if(router === 'GameMessagePush') {
			if(msg.type === NNProto.GAME_STATUS_PUSH) {
				if (msg.data.gameStatus == NNProto.gameStatus.ROB_BANK && NNModel.getIsPlayingByChairID(NNModel.getMyChairID())) {
					let robBankLimit = NNModel.getRobBankLimit();
					if (robBankLimit > 0 && NNModel.isUnionCreate()) {
						let user = NNModel.getUserByChairID(NNModel.getMyChairID());
						if (user.userInfo.score < robBankLimit) {
							// this.node.active = false;
							// Global.DialogManager.addTipDialog(`金币不足${robBankLimit},不能抢庄`);
							for (let i = 1; i <= 4; ++i) {
								cc.find("Button"+i, this.node).active = false;
							}
							this.node.active = true;
						}
						else {
							for (let i = 1; i <= 4; ++i) {
								cc.find("Button"+i, this.node).active = true;
							}
							this.node.active = true;
						}

					}
					else {
						this.node.active = true;
					}
				}
				else {
					this.node.active = false;
				}
			}
			else if(msg.type === NNProto.GAME_ROB_BANK_PUSH) {
				if (msg.data.chairID == NNModel.getMyChairID()) {
					this.node.active = false;
				}
				let audio = 'Game/Niuniu/NNAudio/woman/qiangzhuang_0';
				if (msg.data.robScale == 0) {
					audio = 'Game/Niuniu/NNAudio/woman/buqiang_0';
				}
				let user = NNModel.getUserByChairID(msg.data.chairID);
				if (user && user.userInfo.sex == 0) {
					audio = audio.replace(/woman/, "man");
				}
				Global.AudioManager.playSound(audio);
			}
		} 
	},

	onButtonClick: function (event, param) {
        Global.AudioManager.playCommonSoundClickButton();
		let scale = parseInt(param);
		let sendData = NNProto.gameRobBankNotify(scale);
		Global.NetworkManager.notify(GameMessageRouter, sendData);
		this.node.active = false;
	},

	onDestroy: function () {
		Global.MessageCallback.removeListener('RoomMessagePush', this);
		Global.MessageCallback.removeListener('GameMessagePush', this);
		Global.MessageCallback.removeListener('ReConnectSuccess', this);
	},
});
